<?php

namespace app\common\model;

use think\Model;
use think\Db;
use think\Config;

class ProjectEvent extends Model {

    // 指定表名,不含前缀
    protected $name = 'project_event';

    /**
     * 根据参数获取列表
     * @param array|null $arg
     * @param string $field
     * @param int $argor
     * @return array|bool|false|\PDOStatement|string|\think\Collection
     */
    public function getListByArg(array $arg=null,$field='*',$argor=1){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->where($argor)->field($field)->select();
        return $arg;
    }
    public function getListByArgOne(array $arg=null,$field='*'){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->field($field)->find();
        return $arg;
    }

    public function getChildAdminReg(){
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
        $cplaform = '1';
        if (count($child_plaform)>1){
            foreach ($child_plaform as $kc=>$kv)
            {
                $cplaform .=",'".$kv."' ";
            }
            $cplaform .=",'".$kv."'";
        }else{
            $cplaform = implode(',',$child_plaform);
            $cplaform = "'".$cplaform."'";
        }
        return $cplaform;
    }
    /* 获取USER表中人所有用户到培训室 */

    public function getAllUserToBasic($trainingid, $userid = 0, $status, $company_id) {
        if (empty($trainingid))
            return false;
        $cplaform = $this->getChildAdminReg();
//        $sql = "insert into ex_event_user(identity_id,username,eventid,usertruename,company,idcard,departmentid,phone,company_id,reg) ";
        $sql = "(select  b.id  identity_id,a.username," . $trainingid . " eventid,a.realname usertruename,b.company_name company,a.userid gdl_userid,ep.id plaform_id,a.idcard,b.departmentid,a.phone,b.company_id,b.admin_reg  from ex_user a left join ex_user_company b on a.userid = b.userid left join ex_plaform as ep on b.admin_reg=ep.admin_reg  where a.isdelete=0 and a.status=1 and b.admin_reg  in ({$cplaform})  ";

        if (is_array($userid)) {
            $userid = implode(',', $userid);
        } else {
            $userid = 0;
        }
        if (!$status) { //等于1表示所有
            $sql .= ' and b.id in(' . $userid . ')';
        }

        $sql .= "and b.id not in (select identity_id from ex_event_user where eventid='{$trainingid}' ";

        if (!empty($userid))
            $sql .= " and b.id in(" . $userid . ")";
        $sql .= "))";
        $res = $this->query($sql);
        if (empty($res)){
            return false;
        }
        //获取当前平台的身份ID
        $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from ex_event_user where eventid='{$trainingid}')";
        $res1 = $this->query($sql1);
        foreach ($res as $key =>$valuse ){
            foreach ($res1 as $key1 => $val1 ){
                if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                    unset($res[$key]);
                }
            }
        }
//        var_dump($res);die;
        if (!empty($res)){
            $value_str = '';
            foreach($res as $v){
                $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",'".$v['username']."',".$v['eventid'].",'".$v['usertruename']."',".$v['company_id'].",'".$v['company']."','".$v['idcard']."','".$v['phone']."','".$v['admin_reg']."',".$v['departmentid']."),";
            }

            $valueStr = rtrim($value_str,',');
            $sql2 = "INSERT into ex_event_user(identity_id,gdl_userid,plaform_id,username,eventid,usertruename,company_id,company,idcard,phone,reg,departmentid) values $valueStr";
//              var_dump($sql);die;
            $this->execute($sql2);
        }
        return true;
    }

    /* 获取USER表中人满足条件用户到培训室 */

    public function getAllUserToMARK($trainingid, $userid = 0, $status, $company_id,$parentid=0) {
        if (empty($trainingid)) {
            return false;
        }

        $cplaform = $this->getChildAdminReg();
//        $sql = "insert into ex_event_user(identity_id,username,eventid,usertruename,company,idcard,departmentid,phone,company_id,reg) ";
        $sql = "(select  b.id  identity_id,a.username," . $trainingid . " eventid,a.realname usertruename,b.company_name company,a.userid gdl_userid,ep.id plaform_id,a.idcard,b.departmentid,a.phone,b.company_id,b.admin_reg from ex_user a left join ex_user_company b on a.userid = b.userid  left join ex_plaform as ep on b.admin_reg=ep.admin_reg where a.isdelete=0 and a.status=1 and b.admin_reg   in ({$cplaform}) "; //排除超级管理员


        switch ($status) {
            case '3':
                if ($userid) {
                    $sql .= ' and project_id=' . $userid;
				}
				if ($parentid) {
					$sql .= ' and b.groupid=' . $parentid;
				}
                break;

            case '4':
                $sql .= ' and b.specialty_id=' . $userid;
                break;
        }

        $sql .= " and b.id not in (select identity_id from ex_event_user where eventid='{$trainingid}' and reg='" . $company_id . "'";


        $sql .= "))";

//        var_dump($sql);die;
        $res = $this->query($sql);
        if (empty($res)){
            return false;
        }
        //获取当前平台的身份ID
        $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from ex_event_user where eventid='{$trainingid}')";
        $res1 = $this->query($sql1);
        foreach ($res as $key =>$valuse ){
            foreach ($res1 as $key1 => $val1 ){
                if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                    unset($res[$key]);
                }
            }
        }
//        var_dump($res);die;
        if (!empty($res)){
            $value_str = '';
            foreach($res as $v){
                $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",'".$v['username']."',".$v['eventid'].",'".$v['usertruename']."',".$v['company_id'].",'".$v['company']."','".$v['idcard']."','".$v['phone']."','".$v['admin_reg']."',".$v['departmentid']."),";
            }

            $valueStr = rtrim($value_str,',');
            $sql2 = "INSERT into ex_event_user(identity_id,gdl_userid,plaform_id,username,eventid,usertruename,company_id,company,idcard,phone,reg,departmentid) values $valueStr";
//              var_dump($sql);die;
            $this->execute($sql2);
        }

        return true;
    }
	
	
	
	/* 通过工程导入用户 */

    public function Projectintr($trainingid, $pid = 0, $status, $company_id,$adminreg,$plaform) {
        if (empty($trainingid))
            return false;
        $cplaform = $this->getChildAdminReg();
		$child_plaform = getChildAdminRegByPlaformId($this->plaform);

//        $sql = "insert into ex_event_user(identity_id,username,eventid,usertruename,company,idcard,departmentid,phone,company_id,reg) ";
        $sql = "(select  b.id  identity_id,a.username," . $trainingid . " eventid,a.realname usertruename,b.company_name company,a.userid gdl_userid,ep.id plaform_id,a.idcard,b.departmentid,a.phone,b.company_id,b.admin_reg  from ex_user a left join ex_user_company b on a.userid = b.userid left join ex_plaform as ep on b.admin_reg=ep.admin_reg  where a.isdelete=0 and a.status=1 and b.admin_reg  in ({$cplaform})  ";

		 if (is_array($pid)) {
            $eventid = implode(',', $pid);
			/****开始检查注册***/
			$identity_id = array();
			$EventUser = new \app\common\model\EventUser();
			$user_cid = $EventUser->where(['eventid'=>['in',$eventid]])->field('distinct identity_id')->select();
			foreach($user_cid as $idv){
				$identity_id[] = $idv['identity_id'];
			}
			if(empty($identity_id)){
				return 1;
			}
			
				/****查询那些用户没有在当前平台*****/
					/*$users = new \app\common\model\UserCompany();
					$UserModel = new \app\common\model\User();
					$useridc = array(); //没有在当前平台的userid\
					$useriall = array(); //所有userid
					$useridcompaid = array(); //当前平台的用户id
					$user = $users->where(['id'=>['in',$identity_id]])->field('distinct userid')->select();
					$usercompany = $users->where(['id'=>['in',$identity_id],'admin_reg'=>$adminreg])->field('distinct userid')->select();
					
					
					foreach($usercompany as $companyv){
						$useridcompaid[] = $companyv->userid;
					}

					foreach($user as $userv){
						if(!in_array($userv->userid,$useridcompaid)){
							$useridc[] = $userv->userid;
							$user = $UserModel->where(['userid'=>$userv->userid])->find();*/
							/****开始添加***/
						/*	
							$userCompanyData['userid'] =$userv->userid;
							$userCompanyData['plaform_id'] = $plaform;
							$userCompanyData['admin_reg'] = $adminreg;
							$userCompanyData['update_time'] = time();
							$userCompanyData['status'] = 0;
							$userCompanyData['company_name'] = $user['company'];
							$userCompanyData['specialty'] = $user['specialty'];
							$userCompanyData['specialty_id'] = $user['specialty_id'];
							$users->insert($userCompanyData);
							usleep(100);*/
							/*****end****/
						/*}
						$useriall[] = $userv->userid;
					}
	

						$brand_new = array(); //准备用来保存身份id
						$usercompany = $users->where(['userid'=>['in',$useriall],'admin_reg'=>$adminreg])->field('distinct id')->select();

						foreach($usercompany as $usercompany_v){
							$brand_new[] = $usercompany_v->id;
						}
						$userid = implode(',',$brand_new);
*/
			$userid =implode(',',$identity_id);
				/****查询那些用户没有在当前平台 end*****/
			/****开始检查注册 end***/
        } else {
            $userid = 0;
        }
	
       



       
        $sql .= " and b.id in(" . $userid . ")";
        $sql .= " and b.id not in (select identity_id from ex_event_user where eventid='{$trainingid}' ";//and reg='" . $company_id . "'


        $sql .= "))";
	

        $res = $this->query($sql);

        if (empty($res)){
            return 1;
        }
        //获取当前平台的身份ID
        $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (".$userid.")";
		
        $res1 = $this->query($sql1);
        foreach ($res as $key =>$valuse ){
            foreach ($res1 as $key1 => $val1 ){
                if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                    unset($res[$key]);
                }
            }
        }
     //   var_dump($res);die;
        if (!empty($res)){
            $value_str = '';
            foreach($res as $v){
                $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",'".$v['username']."',".$v['eventid'].",'".$v['usertruename']."',".$v['company_id'].",'".$v['company']."','".$v['idcard']."','".$v['phone']."','".$v['admin_reg']."',".$v['departmentid']."),";
            }

            $valueStr = rtrim($value_str,',');
            $sql2 = "INSERT into ex_event_user(identity_id,gdl_userid,plaform_id,username,eventid,usertruename,company_id,company,idcard,phone,reg,departmentid) values $valueStr";
        //     var_dump($sql2);die;
            $this->execute($sql2);
        }
        return true;
    }

}
